package com.tcm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcm.entity.Organization;
import com.tcm.entity.dto.OrgUpdateDto;
import com.tcm.entity.vo.OrgResult;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.mapper.OrganizationMapper;
import com.tcm.service.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/4 19:44
 */
@Slf4j
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    /**
     * 顶级节点层级
     */
    private static final int TOP_NODE_GRADE = 0;

    /**
     * 学院类型
     */
    private static final int COLLEGE_TYPE = 0;

    /**
     * 专业类型
     */
    private static final int MAJOR_TYPE = 1;

    @Autowired
    private OrganizationMapper orgMapper;

    /**
     * 通过父级ID获取组织机构节点
     * @param parentId 父级ID
     * @return 节点数据
     */
    @Override
    public List<OrgResult> getOrgNodesByParentId(Long parentId){
        List<OrgResult> results = new ArrayList<>();
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        if (parentId == null) {
            // 为空则获取顶级节点，即grade为0
            queryWrapper.eq(Organization::getGrade, TOP_NODE_GRADE);

        }else {
            // 查找指定节点的子节点
            queryWrapper.eq(Organization::getParentId, parentId);
        }
        queryWrapper.orderByAsc(Organization::getSequence);
        List<Organization> list = orgMapper.selectList(queryWrapper);
        for (Organization organization : list) {
            OrgResult orgResult = new OrgResult();
            BeanUtils.copyProperties(organization, orgResult);
            results.add(orgResult);
        }
        return results;
    }

    /**
     * 添加组织机构
     * @param orgUpdateDto 组织机构更新数据封装类
     * @param currUserId 当前登录用户ID
     * @return 添加结果，true为成功
     */
    @Override
    @Transactional
    public boolean addOrganization(OrgUpdateDto orgUpdateDto, Long currUserId){
        Organization organization = new Organization();
        BeanUtils.copyProperties(orgUpdateDto, organization);

        Organization parent = orgMapper.selectById(organization.getParentId());
        organization.setGrade(parent.getGrade() + 1);// 设置层级
        organization.setSequence(organization.getSequence() == null ? 1 : organization.getSequence());// 设置默认排序

        organization.setCreateUser(currUserId);
        organization.setUpdateUser(currUserId);

        int res = orgMapper.insert(organization);
        return res == 1;
    }

    /**
     * 修改组织机构节点信息
     * @param orgUpdateDto 组织机构节点数据封装类
     * @param currUserId 当前登录用户ID
     * @return 修改结果，true为成功
     */
    @Override
    @Transactional
    public boolean modifyOrganization(OrgUpdateDto orgUpdateDto, Long currUserId){
        orgUpdateDto.setSequence(orgUpdateDto.getSequence() == null ? 1 : orgUpdateDto.getSequence());

        Organization organization = orgMapper.selectById(orgUpdateDto.getId());
        if (organization == null) {
            return false;
        }
        BeanUtils.copyProperties(orgUpdateDto, organization);

        organization.setUpdateUser(currUserId);
        int res = orgMapper.updateById(organization);
        return res == 1;
    }

    @Override
    @Transactional
    public boolean removeOrganization(Long id, Long currUserId) throws DatabaseOperationException{
        Organization organization = orgMapper.selectById(id);
        if (organization == null) {
            return false;
        }
        organization.setUpdateUser(currUserId);
        int res1 = orgMapper.updateById(organization);
        int res2 = orgMapper.deleteById(id);
        if (res1 != res2) {
            throw new DatabaseOperationException("数据库操作异常");
        }
        return true;
    }

    /**
     * 获取学院列表
     * @return 学院列表
     */
    @Override
    public List<Organization> getCollegeList(){
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getType, COLLEGE_TYPE);
        return orgMapper.selectList(queryWrapper);
    }

    /**
     * 获取学院列表
     * @return 学院列表
     */
    @Override
    public List<Organization> getMajorList(){
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getType, MAJOR_TYPE);
        return orgMapper.selectList(queryWrapper);
    }

}
